<template>
  <div class="container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>关键字</span>
        <el-input
          v-model="keyword"
          placeholder="根据编号搜索"
          class="shurukuang"
        ></el-input>
        <el-button style="float:right" type="primary" @click="search"
          >搜索</el-button
        >
        <el-button style="float:right" class="qingchu" @click="eliminate"
          >清除</el-button
        >
      </div>
      <el-row>
        <el-col :span="24"
          ><div class="grid-content bg-purple-dark">
            <i class="el-icon-info"></i>
            <span style="color:#909399">数据一共178条</span>
          </div>
        </el-col>
      </el-row>
      <br />
      <div v-for="o in 1" :key="o" class="text item">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="编号" width="200px">

          </el-table-column>
          <el-table-column prop="name" label="题型">
            <template slot-scope="tableData">
              <span>{{ tableData.row.questionType }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="题目编号" width="200px">
            <template #default='scope'>
              <div v-html="getList(scope.row.questionIDs)" @click="previewBtn($event)" class="inp"></div>
              <!-- <span @click="previewBtn(tableData.row)" class="inp">{{ tableData.row.id }}</span> -->
            </template>
          </el-table-column>
          <el-table-column prop="inputTime" label="录入时间">
            <template slot-scope="tableData">
              <span>{{ tableData.row.addTime }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="datishijian" label="答题时间">
            <template slot-scope="tableData">
              <span>{{ tableData.row.totalSeconds }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="zhengquelv" label="正确率(%)">
            <template slot-scope="tableData">
              <span>{{ tableData.row.accuracyRate }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="lururen" label="录入人">
            <template slot-scope="tableData">
              <span>{{ tableData.row.userName }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="caozuo" label="操作">
            <template #default="scope">
              <i
                class="el-icon-delete"
                style=" color: red;"
                type="text"
                @click="del(scope.row.id)"
              ></i>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          layout="prev, pager, next,jumper,sizes"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-size="params.pagesize"
          :current-page="params.page"
          :page-sizes="[5, 10, 15, 20, 25, 30]"
          style="float:right"
        >
        </el-pagination>
      </div>
      <questionsPreview :showDilog='showDilog' @handle-Close="handleClose" :previewList='previewList'></questionsPreview>
    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'
import questionsPreview from '../components/questions-preview'
export default {
  data () {
    return {
      input: '',
      tableData: [], // 所有数据
      params: {
        page: 1, // 页码
        pagesize: 10 // 每条页数
      },
      total: 0, // 总条数
      cuntId: 0,
      keyword: '', // 查找
      showDilog: false, // 试题预览弹窗状态
      previewList: {} // 预览数据
    }
  },
  components: {
    questionsPreview
  },
  mounted () {
    this.shuju()
  },
  methods: {
    async shuju () { // 获取数据
      const res = await randoms(this.params)

      this.tableData = res.data.items
      console.log(this.tableData)
      this.total = res.data.counts
    },
    handleCurrentChange (val) {
      // 当前页
      console.log(val)
      this.params.page = val
      this.shuju()
    },
    handleSizeChange (val) {
      // 每页条数
      this.params.pagesize = val
      // console.log(this.params.pageSize)
    },
    del (id) {
      // 删除
      console.log(id)
      this.cuntId = id
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await removeRandoms({ id: this.cuntId })
          // 更新列表
          this.shuju()
          // 提示用户
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async search () {
      // 搜索
      // tableData.row.id
      // keyword = this.tableData.row.id
      const res = await randoms({ keyword: this.keyword, page: this.params.page, pagesize: this.params.pagesize })
      console.log(res)
      this.tableData = res.data.items
    },
    eliminate () {
      // 清除
      this.keyword = ''
    },
    submitForm (submitForm) {
      submitForm = false
    },
    async previewBtn (e) { // 试题预览
      // console.log(e)
      // console.log(e.target.getAttribute('data-id'))
      // this.cuntId = id

      const res = await detail({ id: e.target.getAttribute('data-id') })

      this.previewList = res.data
      // console.log(this.previewList)w
      this.showDilog = true
    },
    handleClose () { // 预览弹窗关闭
      this.showDilog = false
    },
    getList (list) {
      var arr = []
      list.forEach(item => {
        arr.push(`<p data-id=${item.id}>${item.number}<p>`)
      })
      return arr.join('')
    }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 20px 20px 0 20px;
}
.inp {
  color:blue;
  cursor: pointer;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.shurukuang {
  width: 200px;
  margin-left: 20px;
}

.bg-purple-dark {
  background: #f4f4f5;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.el-icon-info {
  line-height: 45px;
  color: #909399;
  margin-left: 20px;
  margin-right: 10px;
}
</style>
